<template>
	<view class="content">
		<view class="head">
			<u-navbar leftIcon="arrow-leftward" :placeholder='true' @leftClick="leftClick" title="訂單月匯總">
			</u-navbar>
		</view>
		<view class="search">
			<u-search :show-action="false" borderColor="#7A7DEE" bgColor="#FFFFFF" placeholder="請輸入訂單號查詢"
				v-model="keyword" @search="search"></u-search>
			<view class="title-box u-flex" @click="filter">
				<text class="filter-title">篩選</text>
				<view class="p-box u-flex-col u-m-l-6">
					<text class="iconfont icon-shaixuan"></text>
				</view>
			</view>
		</view>
		<view class="nav">
			<view :class="['nav-item', { active: currentIndex === index }]" @click="changeTab(index)"
				v-for="(item,index) in tabs" :key="index">
				{{item}}
			</view>
		</view>

		<view class="main bg">
			<view class="order-item" v-for="(item,index) in ordermonthlist" :key="index" @click="todetails(item)">
				<view class="">
					時間：<text style="color: #333333;">{{item.month}}</text>
				</view>
				<view style="margin-top: 12rpx;">
					小計：<text style="color: #4949C3;">MOP {{item.money}}</text>
				</view>
			</view>
		</view>
		<u-popup :show="show" mode="right" @close="closeWindows" @open="open">
			<view class="dialog">
				<view>
					<view class="title">訂單號</view>
					<view class="cbox">
						<u-search :showAction="false" placeholder="請輸入訂單號查詢" v-model="keyword"></u-search>
					</view>
				</view>
				<view>
					<view class="title">日期</view>
					<!-- 日期选择器 -->
					<view class="cbox1">
						<view class="cbox-item" @click="toStart">
							<view class="time-item">
								{{beginTime || '开始日期'}}
							</view>
						</view>
						<view class="lines" style="color:#BEBEBE ;">-</view>
						<view class="cbox-item" @click="toEnd">
							<view class="time-item">
								{{endTime ||'结束日期'}}
							</view>
						</view>
					</view>
				</view>
				<view>
					<view class="title">部門</view>
					<view class="cel_box" @click="isPart = !isPart">
						<!-- 下拉选择器 -->
						<view class="sel_ipt">
							<u--input placeholder="请选择部门" border="none" v-model="department" shape="circle"></u--input>
						</view>
						<!-- <image src="../../../static/images/unfold_gray.png" style="width: 30rpx;height: 30rpx;"></image> -->
						<u-icon name="arrow-down" size="28rpx"></u-icon>
						<view v-if="isPart" class="part_box">
							<scroll-view v-if="isPart" scroll-y="true" style="height: 300rpx;">
								<view class="part_item" :class="actIndex==index?'act_part':''"
									v-for="(item,index) in memberlist" :key="index" @click="lookOne(item,index)">
									{{item.departmentname}}
								</view>
							</scroll-view>
						</view>
					</view>
				</view>
				<view class="btn">
					<view class="btn1" @click="reset">
						重置
					</view>
					<view class="btn2" @click="submit">
						确定
					</view>
				</view>
			</view>
		</u-popup>
		<u-datetime-picker ref="datetimePicker" :show="showTime" v-model="valueStart" mode="date" :formatter="formatter"
			title="开始时间" @confirm="confirmStart" @cancel="cancelStart"></u-datetime-picker>
		<u-datetime-picker ref="datetimePicker" :show="showTime1" v-model="valueEnd" mode="date" :formatter="formatter"
			title="结束时间" @confirm="confirmEnd" @cancel="cancelEnd"></u-datetime-picker>

	</view>
</template>

<script>
	import {
		ordermonth
	} from '@/network/api/user.js'
	import {
		getmember,
	} from '@/network/api/cart.js'
	export default {
		data() {
			return {
				ordermonthlist: [],
				choiceContent: "请选择", //选择的内容
				choiceIndex: 0, //选择位置
				show: false,
				tabs: ['全部', '已發貨', '已收貨', '全單取消', '要求退貨'],
				currentIndex: 0,
				keyword: "",
				data: {
					lang: 'cn',
					release: '',
					relend: '',
					order_sn: '',
					department: '',
					user_id: '',
					order_status: '',
				},
				memberlist: [],
				valueStart: Number(new Date()),
				valueEnd: Number(new Date()),
				beginTime: '',
				endTime: '',
				showTime: false,
				showTime1: false,
				department: '', //部门
				isPart: false, //选择部门
				actIndex: 0,
			}
		},
		onShow() {
			this.getmemberlist()
			this.getordermonth()
		},
		methods: {
			async getmemberlist() {
				let company_id = uni.getStorageSync('user').userinfo.company_id
				const res = await getmember(company_id)
				this.memberlist = res.data.department
			},
			search() {
				this.data.order_sn = this.keyword
				this.getordermonth()
			},
			async getordermonth() {
        //转圈
        uni.showLoading({
          title: '加载中'
        })
				const res = await ordermonth(this.data)
        uni.hideLoading()
				this.ordermonthlist = res.data.order
			},
			todetails(item) {
				uni.navigateTo({
					url: `/pages/mine/gather/details?month=${item.month}`
				})
			},
			closeWindows() {
				this.show = false
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			toStart() {
				this.showTime = true
				this.showTime1 = false
			},
			toEnd() {
				this.showTime1 = true
				this.showTime = false
			},
			confirmStart(e) {
				let date = new Date(e.value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				this.beginTime = y + '-' + MM + '-' + d; //年月日
				this.data.release = this.beginTime
				this.showTime = false
			},
			cancelStart(e) {
				this.showTime = false
			},
			confirmEnd(e) {
				let date = new Date(e.value);
				let y = date.getFullYear();
				let MM = date.getMonth() + 1;
				MM = MM < 10 ? ('0' + MM) : MM; //月补0
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d; //天补0
				this.endTime = y + '-' + MM + '-' + d; //年月日
				this.data.relend = this.endTime
				this.showTime1 = false
			},
			cancelEnd(e) {
				this.showTime1 = false
			},
			lookOne(item, index) {
				this.department = item.departmentname
				this.data.department = item.department_id
				this.actIndex = index
			},
			open() {

			},
			reset() {
				this.keyword = '',
					this.data.order_sn = '',
					this.data.release = '',
					this.data.relend = '',
					this.data.department = '',
					this.department = '',
					this.endTime = '',
					this.beginTime = ''
				this.ordermonthlist = []
				this.getordermonth()
				this.show = false
			},
			submit() {
				this.ordermonthlist = []
				this.getordermonth()
				this.show = false
			},
			filter() {
				this.show = true
			},
			changeTab(index) {
				this.currentIndex = index;
				if (this.currentIndex == 0) {
					this.data.order_status = ''
				}
				if (this.currentIndex == 1) {
					this.data.order_status = 4
				}
				if (this.currentIndex == 2) {
					this.data.order_status = 5
				}
				if (this.currentIndex == 3) {
					this.data.order_status = 6
				}
				if (this.currentIndex == 4) {
					this.data.order_status = 7
				}
				if (this.currentIndex == 5) {
					this.data.order_status = 8
				}
				this.getordermonth()
			},
			leftClick() {
				uni.switchTab({
					url: '/pages/mine/index'
				})
			}
		}
	}
</script>

<style lang="scss">
	@import "@/static/iconfont/iconfont.css";

	page {
		background: #f5f6f9;
	}

	.bg {
		background-color: white;
	}

	.content {
		.search {
			background-color: white;
			display: flex;
			align-items: center;
			padding: 15rpx 32rpx;

			.title-box {
				display: flex;
				margin-left: 22rpx;
				align-items: center;
			}
		}

		.nav {
			background-color: white;
			height: 88rpx;
			line-height: 88rpx;
			display: flex;
			justify-content: space-around;
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
		}

		.active {
			font-size: 28rpx;
			font-family: PingFang SC;
			font-weight: bold;
			color: #1F1F1F;
			border-bottom: 5rpx solid #645AED;
		}

		.main {
			margin-top: 16rpx;
			padding: 0 32rpx;

			.order-item {
				padding: 32rpx 0;
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #999999;
				border-bottom: 1rpx solid #DEDEDE;
				;
			}

			.order-item:last-child {
				border-bottom: none;
			}
		}

		.dialog {
			// padding: 0 31rpx;
			width: 640rpx;

			.title {
				margin: 0 31rpx;
				margin-top: 80rpx;
				margin-bottom: 31rpx;
				font-size: 26rpx;
				font-family: PingFang SC;
				font-weight: bold;
				color: #000000;
			}

			.cbox {
				margin: 0 31rpx;
				display: flex;
				justify-content: space-between;
				flex-wrap: wrap;

				.citem {
					margin-bottom: 32rpx;
					text-align: center;
					align-items: center;
					width: 185rpx;
					height: 54rpx;
					line-height: 54rpx;
					background: #F5F6F9;
					border-radius: 16rpx;
					font-size: 26rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #000000;
				}

				.active {
					border: 1px solid #645AED;
					background: #E5E7FA;
					color: #645AED;
				}
			}

			.cbox1 {
				margin: 0 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;

				.lines {
					padding: 10rpx;
				}

				.cbox-item {
					.time-item {
						width: 220rpx;
						background-color: #f2f2f2;
						height: 64rpx;
						border-radius: 32rpx;
						text-align: center;
						line-height: 64rpx;
						font-size: 28rpx;
						font-family: PingFang SC;
						font-weight: 500;
						color: #999999;
					}
				}
			}

			.cel_box {
				margin-left: 32rpx;
				display: flex;
				align-items: center;
				justify-content: space-between;
				width: 500rpx;
				height: 60rpx;
				border-radius: 30rpx;
				background-color: #f2f2f2;
				padding: 0 20rpx;
				position: relative;
			}

			.part_box {
				position: absolute;
				left: 20rpx;
				top: 70rpx;
				width: 400rpx;
				height: 300rpx;
				box-shadow: 0rpx -3rpx 6rpx 0rpx rgba(0, 0, 0, 0.40);

				.part_item,
				.act_part {
					width: 320rpx;
					height: 60rpx;
					font-size: 24rpx;
					color: #000;
					line-height: 60rpx;
					padding: 0 40rpx;
				}

				.act_part {
					background-color: #f2f2f2;
				}
			}

			.btn {
				position: absolute;
				left: 31rpx;
				right: 31rpx;
				bottom: 15rpx;
				display: flex;
				justify-content: space-between;

				.btn1 {
					width: 280rpx;
					height: 68rpx;
					text-align: center;
					line-height: 68rpx;
					border: 1px solid #CBCBCB;
					opacity: 1;
					border-radius: 24rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #3A3A3A;
				}

				.btn2 {
					width: 280rpx;
					height: 68rpx;
					text-align: center;
					background: #645AED;
					line-height: 68rpx;
					border: 1px solid #CBCBCB;
					opacity: 1;
					border-radius: 24rpx;
					font-size: 28rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #FFFFFF;
				}
			}
		}

	}
</style>
